<?php

namespace app\admin\model\chase;

use app\api\model\Wechat;
use EasyWeChat\Factory;
use think\Model;
use traits\model\SoftDelete;

class RealOrder extends Model
{

    use SoftDelete;

    

    // 表名
    protected $table = 'real_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text',
        'payment_text'
    ];
    

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
    }

    public function getPaymentList()
    {
        return ['1' => __('Payment 1'), '2' => __('Payment 2')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPaymentTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['payment']) ? $data['payment'] : '');
        $list = $this->getPaymentList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function service()
    {
        return $this->hasMany(RealOrderService::class, 'order_id', 'id');
    }

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    public function userCoupon()
    {
        return $this->hasOne(UserCoupon::class, 'id', 'user_coupon_id');
    }


    /**
     * 创建小程序码
     */
//    public static function createCode($order_id)
//    {
//        //生成小程序码
//        $dir = ROOT_PATH . 'public/uploads/realorder/';
//        $data = [
////            'page' => 'pages/scan_pay_order/index',
//            'scene' => 'order_id='.$order_id,
//        ];
//        $filePath = $dir . md5($data['scene']) . '.png';
//        //获取小程序码
//        $wechat = new Wechat();
//        $res = $wechat->getORCode($data);
////        return $res;
//        if (isset($res['errcode'])) {
//            return ['errcode' => $res['errcode'], 'errmsg' => $res['errmsg']];
//        }
//        if (!is_dir($dir)) {
//            mkdir($dir, 0777, true);
//        }
//        //将二进制小程序码转换为图片保存
//        $file = fopen($filePath, 'w');
//        fwrite($file, $res);
//        fclose($file);
////        file_put_contents($filePath, $res);
//
//        $image = str_replace(ROOT_PATH . 'public', '', $filePath);
//        return request()->domain(). $image;
//    }

    public static function createCode($order_id)
    {
        $config = [
            'app_id' => 'wx0763924b70c283ef',
            'secret' => 'cbaea10dca781e650d177a8258b0a348',

            // 下面为可选项
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',

            'log' => [
                'level' => 'debug',
                'file' => __DIR__.'/wechat.log',
            ],
        ];

        $app = Factory::miniProgram($config);

        $scene = 'order_id='.$order_id;
        $page = 'pages/scan_pay_order/index';
        $res = $app->app_code->getUnlimit($scene, [
            'page' => $page
        ]);
        if (is_array($res) && isset($res['errcode']) && $res['errcode']) {
            return ['errcode' => $res['errcode'], 'errmsg' => $res['errmsg']];
        }
        // 保存小程序码到文件
        if ($res instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            $filename = $res->save(ROOT_PATH . 'public/uploads/realorder/', md5($page.$scene).'.png');
            return request()->domain().'/uploads/realorder/'.$filename;
        }
    }
}
